package com.dcy.znoa.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.operRole.SelectOperRoleModel;
import com.dcy.znoa.dao.sys.OperRoleDao;
import com.dcy.znoa.dao.sys.RoleDao;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.sys.OperRole;
import com.dcy.znoa.domain.sys.Role;
import com.dcy.znoa.service.sys.OperRoleService;
import com.google.common.base.Strings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OperRoleServiceImpl implements OperRoleService {

    @Autowired
    OperRoleDao operRoleDao;
    @Autowired
    RoleDao roleDao;
    
    @Override
    public JsonForList selectList(SelectOperRoleModel model) throws InvocationTargetException, IllegalAccessException {
        List<OperRole> list;       
        
        EntityWrapper<OperRole> ew = new EntityWrapper<>();
        ew.eq("oper_id",model.getOperId());
        
        //获取所属角色ID
        list = operRoleDao.selectList(ew);
        
        if (!list.isEmpty()) {
        	List<Long> idList = new ArrayList<>();
        	for (OperRole operRole : list) {
        		idList.add(operRole.getRoleId());
    		}
        	
        	List<Role> roleList;     
        	//查询角色实体
        	roleList = roleDao.selectBatchIds(idList);
        	if (roleList.isEmpty()) {
        		return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
			}else {
				return new JsonForList(roleList, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
			}
        	
		}else {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}

        
    }

    @Override
    public JsonCommon insert(OperRole OperRole) {
        Integer result = operRoleDao.insert(OperRole);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon updateById(OperRole OperRole) {
        Integer result = operRoleDao.updateById(OperRole);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

	@Override
	public JsonCommon delete(Long operId, List<Long> roleIdsList) {
//		Map<String, Object> columnMap = new HashMap<String, Object>();
//		columnMap.put("oper_id", operId);
//		columnMap.put("role_id", roleIdsList);
//		operRoleDao.deleteByMap(columnMap);
		
		EntityWrapper<OperRole> ew = new EntityWrapper<>();
        ew.eq("oper_id",operId) .in("role_id", roleIdsList);
        operRoleDao.delete(ew);       
		
		
		return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}

	@Override
	public JsonCommon deleteByRoleId(Long roleId, List<Long> idsList) {
		EntityWrapper<OperRole> ew = new EntityWrapper<>();
        ew.eq("role_id",roleId) .in("oper_id", idsList);
        operRoleDao.delete(ew);
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
	}


}
